
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;
using Swaf.BizObj;


namespace Swaf.BizObj.xml
{
	/// <summary> Maintains a reference to its parent <c>XmlBusinessObjectIList</c> to get the
	/// <i>current element</i> of the list for the data store of its properties.
	/// *
	/// </summary>
	/// <author>  mdenman
	/// 
	/// </author>
	[Serializable]
	public class XmlDependentBizObj : XmlBaseBusinessObject, IXmlNodeParent
	{
		protected IXmlNodeParent m_parent;
		protected string m_name;
		
		/// <summary>Creates a new instance of XmlIListBusinessObject 
		/// </summary>
		public XmlDependentBizObj(IBizObjDefinition type, IXmlNodeParent parent)
			:this(type, parent, null)
		{
			m_parent = parent;
		}
		public XmlDependentBizObj(IBizObjDefinition type, IXmlNodeParent parent, string name)
			:base(type)
		{
			m_parent = parent;
			m_name = name;
		}
		
		protected internal override XmlElement xmlElement
		{get{
			return m_parent.getChildElement(m_name, false);
		}}

		public XmlElement getChildElement(string name, bool createIfNeeded)
		{
			XmlElement el = null;
			IBizObjField field = (IBizObjField)type.Fields[name];
			if(field != null)
			{
				XmlElement pEl = m_parent.getChildElement(m_name, createIfNeeded);
				if(field.BizType == BizFieldType.NetClass)
					return pEl;
				else if(pEl != null)
				{
					//el = (XmlElement)pEl.SelectSingleNode(name);
					el = Cmn.findChildWithName(pEl, name);
					if(el == null && createIfNeeded)
					{
						el = (XmlElement)pEl.AppendChild(pEl.OwnerDocument.CreateElement(name));
					}
				}
			}
			return el;
		}
		
		public override IBizObj Reference {get{return new XmlBusinessObject(objType, xmlElement);}}
		
		internal IBizObjDefinition objType
		{get{
			return type;
		}}

		public override System.Collections.IEnumerator GetEnumerator()
		{
			return xmlElement.GetEnumerator();
		}
	}
}